<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/8
 * Time: 15:10
 */

// connect 方法 执行异步连接操作。此方法会立刻返回。

///示例 mysql代理

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

require_once "./workerman/Autoloader.php";

// 真实的mysql地址，假设这里是本机3306端口
$REAL_MYSQL_ADDRESS = 'tcp://127.0.0.1:3306';

$proxy = new Worker("tcp://0.0.0.0:6606");

$proxy->onConnect = function ($connection) use ($REAL_MYSQL_ADDRESS) {
//    global $REAL_MYSQL_ADDRESS;

    //异步建立一个到实际mysql服务器的连接
    $connection_to_mysql = new AsyncTcpConnection($REAL_MYSQL_ADDRESS);

    //mysql发来数据的时候，转发给对应的客户端连接

    $connection_to_mysql->onMessage = function ($connection_to_mysql, $buffer) use ($connection) {

        $connection->send($buffer);

    };

    //mysql关闭的时候 同时关闭关闭对应的代理到客户端的连接

    $connection_to_mysql->onClose = function ($connection_to_mysql) use ($connection) {
        $connection->close();
    };

    // mysql连接上发生错误时，关闭对应的代理到客户端的连接
    $connection_to_mysql->onError = function ($connection_to_mysql) use ($connection) {
        $connection->close();
    };

    //执行异步连接

    $connection_to_mysql->connect();


    //客户端发送过来数据的时候  转发给对应的mysql连接

    $connection->onMessage = function ($connection, $buffer) use ($connection_to_mysql) {
        $connection_to_mysql->send($buffer);
    };


    // 客户端断开连接时候 ，关闭对应的mysql连接

    $connection->onClose = function ($connection) use ($connection_to_mysql) {
        $connection_to_mysql->close();
    };


    // 客户端连接发生错误时，断开对应的mysql连接
    $connection->onError = function($connection)use($connection_to_mysql)
    {
        $connection_to_mysql->close();
    };

};

//运行worker
Worker::runAll();